package com.lijiajia.cloud.authserver.security.form;

import com.lijiajia.cloud.authserver.pojo.mongodb.User;
import com.lijiajia.cloud.authserver.security.BaseUser;
import com.lijiajia.cloud.authserver.security.BaseUserService;
import com.lijiajia.cloud.authserver.service.MenuService;
import com.lijiajia.cloud.authserver.service.RoleService;
import com.lijiajia.cloud.authserver.service.UserService;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;

public class UsernameUserService extends BaseUserService implements UserDetailsService {
    private final UserService userService;

    public UsernameUserService(UserService userService, RoleService roleService, MenuService menuService) {
        super(roleService, menuService);
        this.userService = userService;
    }

    @Override
    public BaseUser loadUserByUsername(String username) throws UsernameNotFoundException {
        User user = userService.findByUsername(username)
                .orElseThrow(() -> new UsernameNotFoundException(String.format("用户%s不存在", username)));
        return buildUser(user);
    }
}
